﻿using Coldairarrow.Entity.Base_leasesystem_employee;
using Coldairarrow.Entity.Base_leasesystem_employeesign;
using Coldairarrow.Entity.Base_leasesystem_merchant;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Base_leasesystem_employeesign
{
    public class leasesystem_employeesignBusiness : BaseBusiness<leasesystem_employeesign>, Ileasesystem_employeesignBusiness, ITransientDependency
    {
        public leasesystem_employeesignBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<leasesystem_employeesign>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<leasesystem_employeesign>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<leasesystem_employeesign, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<object> GetDataListOver(PageInput<ConditionDTO> input)
        {
            var obj =  Db.GetIQueryable<leasesystem_employeesign>().Join(Db.GetIQueryable<leasesystem_employee>(), a => a.employeeId, b => b.Id, (a, b) => new
            {
                a.Id,
                b.EmpName,
                b.IdCardNo,
                b.BankCardNo,
                b.EmpPhone,
                a.BeginDate,
                a.SignFiled,
                a.EndDate,
                a.zt,
                b.MID
            }).Join(Db.GetIQueryable<leasesystem_merchant>(), a => a.MID, b => b.Id, (a, b) => new
            {
                a.Id,
                a.EmpName,
                a.IdCardNo,
                a.SignFiled,
                a.BankCardNo,
                a.EmpPhone,
                a.BeginDate,
                a.zt,
                a.EndDate,
                b.customerName
                
            });

            if (!input.Search.EmpName.IsNullOrEmpty()) {
                obj = obj.Where(a=>a.EmpName.Contains(input.Search.EmpName));
            }
            if (!input.Search.EmpPhone.IsNullOrEmpty()) {
                obj = obj.Where(a=>a.EmpPhone == input.Search.EmpPhone);
            }
            if (!input.Search.IdNumber.IsNullOrEmpty()) {
                obj = obj.Where(a=>a.IdCardNo == input.Search.IdNumber);
            }
            if (!input.Search.Keyword.IsNullOrEmpty()) {
                obj = obj.Where(a=>a.customerName.Contains(input.Search.Keyword));
            }
            var result = new
            {
                Data = obj.OrderBy($@"{input.SortField} {input.SortType}").Skip((input.PageIndex - 1) * input.PageRows)
                .Take(input.PageRows),
                Total = obj.Count()
            };
            return result;
        }

        public async Task<leasesystem_employeesign> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(leasesystem_employeesign data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(leasesystem_employeesign data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}